<template>
  <div class="viewContain">
    <el-form :model="listQuery" size="small" label-width="100px">
      <el-row>
        <el-col :xs="{ span: 24 }" :sm="{ span: 12 }" :md="{ span: 6 }" :xl="{ span: 6 }">
          <el-form-item label="地区 :">
            <el-input
              v-model="listQuery.area"
              placeholder="请输入"
              clearable
              @keyup.enter.native="handleSearch"
            />
          </el-form-item>
        </el-col>
        <el-col :xs="{ span: 24 }" :sm="{ span: 12 }" :md="{ span: 12 }" :xl="{ span: 12 }">
          <el-form-item label-width="20px">
            <el-button size="mini" type="primary" @click="handleSearch">查询</el-button>
            <el-button size="mini" type="primary" plain @click="handleReload">重置</el-button>
            <el-button size="mini" type="primary" @click="handleAdd">新增</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <Dialog
      :model-value="dailog.visible"
      :title="dailog.title"
      :visible="dailog.visible"
      :width="dailog.width"
      @handleCloseDialog="handleCloseDialog"
      @handleConfirm="handleConfirm"
    >
      <template #content>
        <el-form
          ref="formData"
          label-width="100px"
          :model="formData"
          :rules="rules"
          size="small"
        >
          <el-row>
            <el-form-item label="重货价格 :" prop="heavyGoods">
              <el-input
                v-model="formData.heavyGoods"
                clearable
                placeholder="请输入重货价格"
                style="max-width: 200px"
              />
            </el-form-item>
            <el-form-item label="轻货价格 :" prop="lightGoods">
              <el-input
                v-model="formData.lightGoods"
                clearable
                placeholder="请输入轻货价格"
                style="max-width: 200px"
              />
            </el-form-item>
            <el-form-item label="起步价 :" prop="startPrice">
              <el-input
                v-model="formData.startPrice"
                clearable
                placeholder="请输入起步价"
                style="max-width: 200px"
              />
            </el-form-item>
            <el-form-item label="发货地区 :" prop="areaCode">
              <el-select
                v-model="formData.areaCode"
                filterable
                clearable
                placeholder="请选择"
                style="width: 200px"
              >
                <el-option
                  v-for="item in addressList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-row>
        </el-form>
      </template>
    </Dialog>
    <el-table
      ref="table"
      v-loading="loading"
      fit
      stripe
      :data="tableData"
      border
      size="mini"
    >
      <el-table-column align="center" label="序号" type="index" width="50">
        <template slot-scope="scope">
          <span>{{ (listQuery.pageNum - 1) * listQuery.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <!-- guangdongPrice: [{ id: 1, startPrice: 5, logisticsName: '安能物流', heavyGoods: 0.65, lightGoods: 130, startPrice: 30 }] -->
      <el-table-column prop="area" align="center" label="地区" />
      <el-table-column prop="heavyGoods" align="center" label="重货价格" width="200" />
      <el-table-column prop="lightGoods" align="center" label="轻货价格" width="200" />
      <el-table-column prop="startPrice" align="center" label="起步价格" width="200" />
      <el-table-column align="center" label="操作" width="300">
        <template slot-scope="scope">
          <el-button class="text-button" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button class="text-button el-button--danger" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total-count="totalCount"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { annengList, provinceList, deliveryList, annengAdd, annengUpdate, unnengDeleteByIds, findByConditonDelivery } from '@/api/index'
import Dialog from '@/components/dailog/index.vue'
import pagination from '@/components/pagination/index.vue'

export default {
  components: {
    Dialog, pagination
  },
  data() {
    return {
      totalCount: 0,
      loading: false,
      options: [],
      addressList: [],
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        area: ''
      },
      tableData: [],
      rules: {
        heavyGoods: [
          { required: true, message: '请输入重货价格', trigger: 'blur' }
        ],
        lightGoods: [
          { required: true, message: '请输入轻货价格', trigger: 'blur' }
        ],
        startPrice: [
          { required: true, message: '请输入起步价', trigger: 'blur' }
        ],
        areaCode: [
          { required: true, message: '请选择发货区域', trigger: 'blur' }
        ]
      },
      rules1: {},
      // guangdongPrice: [{ id: 1, startPrice: 5, logisticsName: '安能物流', heavyGoods: 0.65, lightGoods: 130, startPrice: 30 }] -->
      formData: {
        area: '',
        areaCode: '',
        heavyGoods: '',
        id: '',
        lightGoods: '',
        startPrice: ''
      },
      dailog: {
        // height: '',
        title: '快递新增',
        visible: false,
        visibleAssign: false,
        width: '400px'
      }
    }
  },
  mounted() {
    this.getList()
    this.getAddressList()
  },
  methods: {
    // 快递价格列表
    getList() {
      this.loading = true
      annengList(this.listQuery).then(res => {
        if (res.code === 200) {
          this.tableData = res.data.list
          this.totalCount = res.data.total
          console.log(this.tableData)
          this.loading = false
        } else {
          this.loading = false
        }
      })
    },
    // 省份信息
    getAddressList() {
      const params = { pageNum: 1, pageSize: 100 }
      provinceList(params).then(res => {
        if (res.code === 200) {
          this.addressList = res.data.list
        }
      })
    },

    // 新增
    handleAdd() {
      // alert('开发中,敬请期待~')
      this.formData = {
        area: '',
        areaCode: '',
        heavyGoods: '',
        id: '',
        lightGoods: '',
        startPrice: ''
      }
      this.dailog.visible = true
      this.dailog.title = '新增'
    },
    // 查询
    handleSearch() {
      this.getList()
    },
    // 删除
    handleDelete(id) {
      // alert('开发中,敬请期待~')
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // const params = { ids: id }
        const ids = id
        unnengDeleteByIds({}, ids).then(res => {
          console.log('res====删除结果', res)
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: res.message
            })
            this.getList()
          }
        })
      })
    },
    handleReload() {
      // alert('开发中,敬请期待~')
      this.listQuery.pageNum = 1
      this.listQuery.pageSize = 10
      this.listQuery.area = ''
      this.getList()
    },
    handleEdit(row) {
      this.dailog.visible = true
      this.dailog.title = '编辑'
      this.$nextTick(() => {
        this.formData = JSON.parse(JSON.stringify(row))
        console.log('this.formData开始编辑', this.formData)
      })
    },
    handleConfirm() {
      this.$refs['formData'].validate((valid) => {
        if (valid) {
          this.dailog.visible = false
          // this.formData.firstWeight = Number(this.formData.firstWeight)
          // this.formData.secondWeight = Number(this.formData.secondWeight)
          if (this.dailog.title === '新增') {
            annengAdd(this.formData).then(res => {
              if (res.code === 200) {
                this.getList()
              }
            })
          } else {
            annengUpdate(this.formData).then(res => {
              if (res.code === 200) {
                this.getList()
                this.listQuery.pageNum = 1
                this.listQuery.pageSize = 10
                this.listQuery.area = ''
              }
            })
          }
        }
      })
    },
    handleCloseDialog() {
      this.dailog.visible = false
      console.log(123)
    }
  }
}
</script>

<style scoped>

</style>

